<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                编辑监听器
            </el-header>
            <el-main>
                <el-form ref="editMonitData" :rules="rules" :model="editMonitData" label-width="115px"
                         label-position="left">
                    <el-form-item label="监听器名称 :" prop="name">
                        <el-input v-model="editMonitData.name" placeholder="请输入监听器名称"></el-input>
                    </el-form-item>
                    <el-form-item label="监听器内容 :" prop="scp">
                        <el-input type="textarea" v-model="editMonitData.hitScp" placeholder="请输入监听器内容" :rows="6"></el-input>
                    </el-form-item>
                    <el-form-item label="执行器名称 :" prop="actorName">
                        <el-select v-model="editMonitData.actorName" filterable placeholder="请选择" style="width: 100%;" @change="changeValue">
                            <el-option
                                    v-for="item in actorOptions"
                                    :key="item.uuid"
                                    :label="item.name"
                                    :value="item.name">
                                {{item.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="执行器内容 :" prop="actorScp">
                        <el-input readonly type="textarea" v-model="editMonitData.actorScp" :rows="6"></el-input>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        保存
                    </el-button>
                    <el-button @click="editOut">
                        取消
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../../utils/webUtils.js';

    export default {
        name: "editMonit",
        props: {
            monitUuid: {
                type: String
            }
        },
        data() {
            return {
                editMonitData: {
                    uuid: this.monitUuid,
                    name: '',
                    hitScp:'',
                    actorName:'',
                    actorUuid:'',
                    actorScp:''
                },
                actorOptions:[],
                rules: {
                    name: [
                        {required: true, message: '监听器名称不能为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        //深度监听父组件的props传值
        watch: {
            monitUuid(val) {
                this.editMonitData.uuid = val;
            }
        },
        mounted() {
            this.queryActor();
            this.queryMonit();
        },
        methods: {
            //查询执行器数据
            queryActor() {
                this.axios.request({
                    method: "get",
                    url: "./smt/kdi/actor/search/findAll",
                    params: {}
                }).then(response => {
                    this.actorOptions = response.data.content;
                }).catch(error => {
                    console.log(error);
                });
            },
            changeValue(){
                this.actorOptions.forEach( list =>{
                    if(list.name === this.editMonitData.actorName){
                        this.editMonitData.actorName = list.name
                        this.editMonitData.actorUuid = list.uuid
                        this.editMonitData.actorScp = list.scp
                    }
                })
            },
            //通过uuid查询当前对象信息
            queryMonit() {
                this.axios.request({
                    method: "get",
                    url: "./smt/kdi/monit/" + this.editMonitData.uuid,
                    headers: {},
                    params: {}
                }).then(response => {
                    this.insertMonit(response.data);
                }).catch(error => {
                    console.log(error);
                });
            },
            insertMonit(editMonitData) {
                this.editMonitData.name = editMonitData.name;
                this.editMonitData.hitScp = editMonitData.hitScp;
                this.editMonitData.actorName = editMonitData.actorName;
                this.editMonitData.actorUuid = editMonitData.actorUuid;
                this.editMonitData.actorScp = editMonitData.actorScp;
            },
            submit() {
                this.$refs['editMonitData'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.axios.request({
                        method: "put",
                        url: "./smt/kdi/monit/" + this.editMonitData.uuid,
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: replaceSpacesObj(this.editMonitData)
                    }).then(() => {
                        this.info = '修改成功,两秒后关闭';
                        this.infoShow = true;
                        setTimeout(() => {
                            this.infoShow = false;
                            this.$emit("editState", false);
                            this.info = '';
                        }, 2000);
                    }).catch(error => {
                        console.log(error);
                        this.info = "修改失败";
                        this.infoShow = true;
                    });

                });
            },
            cleanEdit() {
                this.editMonitData = {
                    name: '',
                    hitScp:'',
                    actorName:'',
                    actorUuid:'',
                    actorScp:''
                };
            },
            editOut() {
                this.infoShow = false;
                this.cleanEdit();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 900px;
        height: 100%;
        z-index: 100;
    }


</style>